package com.example.social.api

import okhttp3.OkHttpClient
import okhttp3.Protocol
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object ApiClient {
    private const val BASE_URL = "http://10.0.2.2:8081/api/"  // Android Emulator 使用的本地地址

    // 配置 OkHttpClient，设置超时时间
    val okHttpClient = OkHttpClient.Builder()
        .protocols(listOf(Protocol.HTTP_1_1, Protocol.HTTP_2))  // 强制使用 HTTP 协议
        .build()


    // 配置 Retrofit，传递 OkHttpClient 和 GsonConverterFactory
    private val retrofit: Retrofit by lazy {
        Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(okHttpClient)  // 将 OkHttpClient 传递给 Retrofit

            .addConverterFactory(GsonConverterFactory.create())  // 解析 JSON 的 Converter
            .build()
    }

    val userApiService: UserApiService by lazy {
        retrofit.create(UserApiService::class.java)
    }

    val threadApiService:ThreadApiService by lazy{
        retrofit.create(ThreadApiService::class.java)
    }

    val commentApiService:CommentApiService by lazy{
        retrofit.create(CommentApiService::class.java)
    }

}